//
//  RBTransportRuleView.m
//  5-17底部弹出视图
//
//  Created by RaoBo on 2018/5/17.
//  Copyright © 2018年 关键词. All rights reserved.
//

#define XHHTuanNumViewHight 350.0
#define kWidth  [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

#import "RBTransportRuleView.h"


@interface RBTransportRuleView()
@property(nonatomic, strong) UIView *bgView;
@property(nonatomic, strong) UILabel *titleLb;
/**运费规则*/
@property(nonatomic, strong) UILabel *transportRuleLb;
/**确定按钮*/
@property(nonatomic, strong) UIButton  *sureButton;
/**右上角关闭按钮*/
@property(nonatomic, strong) UIButton *closeBtn;


@end

@implementation RBTransportRuleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubView];
    }
    return self;
}

- (void)setupSubView {
    
    self.frame  = CGRectMake(0, 0, kWidth,XHHTuanNumViewHight);
    self.backgroundColor = [UIColor colorWithRed:110.0/255.0 green:110.0/255.0 blue:110.0/255.0 alpha:0.7];
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView)]];
    
    self.bgView.frame = CGRectMake(0, XHHTuanNumViewHight, kWidth, kHeight - XHHTuanNumViewHight);
    
    self.titleLb.frame = CGRectMake(10, 10, 150, 20);
    [self.bgView addSubview:self.titleLb];
    
    self.transportRuleLb.frame = CGRectMake(10, 40, 200, 200);
    [self.bgView addSubview:self.transportRuleLb];
    
    self.sureButton.frame = CGRectMake(20, XHHTuanNumViewHight - 60, kWidth - 40, 40);
    [self.bgView addSubview:self.sureButton];
    
    self.closeBtn.frame = CGRectMake(_bgView.frame.size.width - 30 - 15, 15, 30, 30);
    [self.bgView addSubview:self.closeBtn];
}

#pragma mark - 懒加载
- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
}
- (UILabel *)titleLb {
    if (!_titleLb) {
        _titleLb = [[UILabel alloc] init];
        _titleLb.text = @"运费规则";
        _titleLb.font = XJFONTSET_M(17.0);
    }
    return _titleLb;
}
- (UILabel *)transportRuleLb {
    if (!_transportRuleLb) {
        _transportRuleLb = [[UILabel alloc] init];
        _transportRuleLb.numberOfLines = 0;
    }
    return _transportRuleLb;
}
- (UIButton *)sureButton {
    if (!_sureButton) {
        _sureButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _sureButton.backgroundColor = XJCOLOR_RGB(255, 218, 67);
        [_sureButton setTitle:@"确定" forState:(UIControlStateNormal)];
        [_sureButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        _sureButton.clipsToBounds = YES;
        _sureButton.layer.cornerRadius = 10;
        [_sureButton addTarget:self action:@selector(disMissView) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _sureButton;
}
- (UIButton *)closeBtn {
    if (!_closeBtn) {
        _closeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_closeBtn setImage:[UIImage imageNamed:@"close_black"] forState:(UIControlStateNormal)];
        [_closeBtn addTarget:self action:@selector(disMissView) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _closeBtn;
}


#pragma mark - 接口
- (void)rb_showInView:(UIView *)view ruleStr:(NSString *)str{
    if (view == nil) {
        return;
    }
    
    [view addSubview:self];
    [view addSubview:self.bgView];
    
    self.transportRuleLb.text = str;

    [self.bgView setFrame:CGRectMake(0, kHeight, kWidth, XHHTuanNumViewHight)];
    
    [UIView animateWithDuration:0.25 animations:^{
        [self.bgView setFrame:CGRectMake(0, kHeight-XHHTuanNumViewHight-XJTopNavH, kWidth, XHHTuanNumViewHight)];
    }];
}
#pragma mark - disMissView
- (void)disMissView{

    [self.bgView setFrame:CGRectMake(0, kHeight-XHHTuanNumViewHight, kWidth, XHHTuanNumViewHight)];

    [UIView animateWithDuration:0.25 animations:^{
        self.alpha = 0.0;
        [self.bgView setFrame:CGRectMake(0, kHeight, kWidth, XHHTuanNumViewHight)];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.bgView removeFromSuperview];
    }];
}
@end
